package com.jieshuibao.service;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.jieshuibao.util.SerializeUtil;
/**
 * 封装redis 缓存服务器服务接口
 * @author drizzt
 */
@Component
public class RedisService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
   
    /**
     * 存取key value
     * @param key 
     * @param value
     * @param liveTime 失效时间  0不失效
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void set(final String key,final String value,final long liveTime){
    	 redisTemplate.execute(new RedisCallback() {
             public Object doInRedis(RedisConnection connection) throws DataAccessException {
                 connection.set(key.getBytes(), value.getBytes());
                 if (liveTime > 0) {
                     connection.expire(key.getBytes(), liveTime);
                 }
                 return 1L;
             }
         });
    }
    
    /**
     * 根据key取value
     * @param key
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public String get(final String key){
    	return (String)redisTemplate.execute(new RedisCallback() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
            	try {
            		return new String(connection.get(key.getBytes()));
				} catch (Exception e) {
					//e.printStackTrace();
				}
            	return null;
            }
        });
    }
    
    /**
     * 线程安全的自增
     * @param key
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void incrby(final String key){
    	redisTemplate.execute(new RedisCallback() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
            	Long incr = connection.incr(key.getBytes());
				return incr;
            }
        });
    }
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public Long getIncrby(final String key){
    	return (Long) redisTemplate.execute(new RedisCallback() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
            	Long incr = connection.incr(key.getBytes());
				return incr;
            }
        });
    }
    /**
     * 线程安全的自减
     * @param key
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void decrby(final String key){
    	redisTemplate.execute(new RedisCallback() {
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
            	Long incr = connection.decr(key.getBytes());
				return incr;
            }
        });
    }
    
    /**
     * 删除key
     * @param key
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void delete(final String key){
    	redisTemplate.execute(new RedisCallback() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
            	return connection.del(key.getBytes());
            }
        });
    }
    
    /**
     * 模糊匹配key
     * @param pattern
     */
    public Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);

    }
    /**
     * 存对象
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void setObject(final String key,final Object value,final long liveTime){
    	redisTemplate.execute(new RedisCallback() {
             public Object doInRedis(RedisConnection connection) throws DataAccessException {
                 connection.set(key.getBytes(), SerializeUtil.serialize(value));
                 if (liveTime > 0) {
                     connection.expire(key.getBytes(), liveTime);
                 }
                 return 1L;
             }
         });
    }
    /**
     * 
     * @param key
     * @param value
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public void setObject(final String key,final Object value){
    	redisTemplate.execute(new RedisCallback() {
             public Object doInRedis(RedisConnection connection) throws DataAccessException {
                 connection.set(key.getBytes(), SerializeUtil.serialize(value));
                 return 1L;
             }
         });
    }
    /**
     * 取对象
     */
    @SuppressWarnings({ "unchecked" })
   	public <T> T getObejct(final String key, Class<T> elementType){
       	return (T) redisTemplate.execute(new RedisCallback<T>() {
               public T doInRedis(RedisConnection connection) throws DataAccessException {
               	try {
               		byte[] value = connection.get(key.getBytes());
               		T unserialize = (T)SerializeUtil.unserialize(value);
               		return unserialize;
   				} catch (Exception e) {
   					//e.printStackTrace();
   				}
               	return null;
               }
           });
       }
}
